package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.db.pojo.PageData;
import cn.com.wangluotongxin.merchants.dto.GoodsInfoDto;
import cn.com.wangluotongxin.merchants.form.GoodsInfoEditForm;
import cn.com.wangluotongxin.merchants.form.GoodsInfoForm;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsInfo;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsInfoService;
import cn.com.wangluotongxin.merchants.vo.GoodsInfoVo;
import cn.com.wangluotongxin.web.utils.TenantAuthUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商户管理-商品信息 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Api(tags = "商户管理-商品信息 相关接口")
@RestController
@RequestMapping("/goods/info")
public class MerchantsGoodsInfoController {

    @Autowired
    private IMerchantsGoodsInfoService goodsInfoService;

    @ApiOperation("分页")
    @GetMapping("/page")
    public AjaxResult<PageData<GoodsInfoVo>> getPage(GoodsInfoDto dto){
        IPage<GoodsInfoVo> page = goodsInfoService.getPage(dto);
        return AjaxResult.success(PageData.build(page));
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public AjaxResult<GoodsInfoVo> detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(goodsInfoService.detail(id));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsInfoForm form) {
        Assert.notNull(form.getCatalogId(), "缺少分类");
        Assert.isTrue(StrUtil.isNotBlank(form.getGoodsName()), "缺少商品名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getGoodsImages()), "缺少图片");

        MerchantsGoodsInfo goodsInfo = goodsInfoService.add(form, TenantAuthUtil.getTenantUser());
        return AjaxResult.success(goodsInfo);
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsInfoEditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.notNull(form.getCatalogId(), "缺少分类");
        Assert.isTrue(StrUtil.isNotBlank(form.getGoodsName()), "缺少商品名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getGoodsImages()), "缺少图片");

        MerchantsGoodsInfo goodsInfo = goodsInfoService.edit(form, TenantAuthUtil.getTenantUser());
        return AjaxResult.success(goodsInfo);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id) {

        goodsInfoService.delete(id);
        return AjaxResult.success();
    }
}